package marke.barak.windows.composit;

import java.io.IOException;

import marek.barak.classes.User;
import marek.barak.windows.main.main;

import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

public class addUsers extends Composite {
	private Text txbFirstName;
	private Text txbLastName;
	
	private User createUser(String f,String l){
		User u = null;
		if((f.length()==0 && l.length()==0) || (f==null)||(l==null)){
			MessageBox msg =  new MessageBox(getShell(),SWT.ICON_ERROR | SWT.ABORT);
			msg.setMessage("User name invalid");
			msg.setText("Error");
			msg.open();
		}else
			u = new User(f,l);
		return u;
	}
	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public addUsers(Composite parent, int style) {
		super(parent, style);
		
		txbFirstName = new Text(this, SWT.BORDER);
		txbFirstName.setBounds(23, 28, 98, 19);
		
		Label lblNewLabel = new Label(this, SWT.NONE);
		lblNewLabel.setBounds(23, 10, 66, 13);
		lblNewLabel.setText("First name");
		
		txbLastName = new Text(this, SWT.BORDER);
		txbLastName.setBounds(23, 72, 98, 19);
		
		Label lblNewLabel_1 = new Label(this, SWT.NONE);
		lblNewLabel_1.setBounds(23, 53, 76, 13);
		lblNewLabel_1.setText("Last name");
		
		Button btnNewButton = new Button(this, SWT.NONE);
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				User u =createUser(txbFirstName.getText(),txbLastName.getText());
				if(u!=null){
					try{
						main.users.addUser(u);
					}catch(IOException ex){
						System.err.println(ex.toString());
					}
				}
				txbFirstName.setText("");
				txbLastName.setText("");
			}
		});
		btnNewButton.setBounds(372, 5, 68, 23);
		btnNewButton.setText("Add");

	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
}
